//
//  ViewController2.m
//  30206
//
//  Created by student on 16/4/7.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController2.h"
#import "DrawView3.h"

@interface ViewController2 ()
{
    CGPoint clipstartpoint;
    CGPoint clipendpoint;
}
@property (weak, nonatomic) IBOutlet DrawView3 *drawview;

@property (weak, nonatomic) IBOutlet UIImageView *imageview;

@property (nonatomic,strong) UIView *clipview;

@end

@implementation ViewController2
-(UIView *)clipview
{
    if(_clipview==nil)
    {
        _clipview=[[UIView alloc]init];
        [self.imageview addSubview:_clipview];
        _clipview.backgroundColor=[UIColor blackColor];
        _clipview.alpha=0.5;
    }
    return _clipview;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)ao:(id)sender
{
    UIGraphicsBeginImageContextWithOptions(self.drawview.bounds.size, YES, 1);
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    [self.drawview.layer renderInContext:ctx];
    
    
    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(img, self, nil, nil);
    NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filepath=[path stringByAppendingPathComponent:@"guo.jpeg"];
    
    NSData *imgdata=UIImageJPEGRepresentation(img, 0.01);
    [imgdata writeToFile:filepath atomically:YES];
    
}
- (IBAction)b2:(id)sender
{
    UIGraphicsBeginImageContextWithOptions(self.drawview.bounds.size, YES, 1);
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    UIBezierPath *path1=[UIBezierPath bezierPathWithOvalInRect:CGRectMake(90, 20, 120, 120)];
    [[UIColor redColor]setFill];
    [path1 addClip];
    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSString *path=[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filepath=[path stringByAppendingPathComponent:@"guo.jpeg"];
    
    NSData *imgdata=UIImageJPEGRepresentation(img, 0.01);
    [imgdata writeToFile:filepath atomically:YES];
    
}
- (IBAction)b3:(id)sender
{
    self.imageview.image=[UIImage imageNamed:@"2.jpeg"];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    clipstartpoint=[touch locationInView:self.imageview];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.clipview.hidden=NO;
    UITouch *touch=[touches anyObject];
    clipendpoint=[touch locationInView:self.imageview];
    self.clipview.frame=CGRectMake(clipstartpoint.x, clipstartpoint.y, clipendpoint.x-clipstartpoint.x, clipendpoint.y-clipstartpoint.y);
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.clipview.hidden=YES;
    UIGraphicsBeginImageContextWithOptions(self.imageview.bounds.size, YES, 1);
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    [self.imageview.layer renderInContext:ctx];
    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIGraphicsBeginImageContextWithOptions(self.clipview.bounds.size, YES, 1);
    CGContextRef ctx2=UIGraphicsGetCurrentContext();
    [img drawAtPoint:CGPointMake(-self.clipview.frame.origin.x, -self.clipview.frame.origin.y)];
    UIImage *img2=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.imageview.image=img2;

}
@end
